package br.net.woodstock.tickettoride.api.spring.security.impl;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.orm.UserStatus;

public class TicketUserDetails implements UserDetails {

	private static final long				serialVersionUID	= 6797136534797989459L;

	private User							user;

	private Collection<GrantedAuthority>	grants;

	public TicketUserDetails(final User user) {
		this.user = user;

		this.grants = new ArrayList<GrantedAuthority>();
		this.grants.add(new SimpleGrantedAuthority(user.getType().name()));
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return this.grants;
	}

	@Override
	public String getPassword() {
		return null;
	}

	@Override
	public String getUsername() {
		return this.user.getName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.user.getStatus() == UserStatus.ACTIVE;
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.user.getStatus() == UserStatus.ACTIVE;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.user.getStatus() == UserStatus.ACTIVE;
	}

	@Override
	public boolean isEnabled() {
		return this.user.getStatus() == UserStatus.ACTIVE;
	}

}
